package org.jash.sportnews2109.vm

import androidx.lifecycle.MutableLiveData
import dagger.hilt.android.lifecycle.HiltViewModel
import org.jash.common.mvvm.BaseViewModel
import org.jash.common.retrofit
import org.jash.sportnews2109.dao.VideoDao
import org.jash.sportnews2109.entity.Page
import org.jash.sportnews2109.entity.Video
import org.jash.sportnews2109.net.APIService
import javax.inject.Inject

@HiltViewModel
class VideoViewModel @Inject constructor(val videoDao: VideoDao):BaseViewModel() {
    val service by lazy { retrofit.create(APIService::class.java)}
    val videoLiveData by lazy { MutableLiveData<Page<Video>>() }
    fun loadVideo(page:Int, size:Int) {
        progress(service.getVideoPage(page, size)) {
            if (it.code == 0) {
                videoLiveData.postValue(it.data)
                videoDao.save(*it.data.records.toTypedArray())
            } else {
                errorLiveData.postValue(RuntimeException(it.msg))
            }
        }
    }
}